/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package queenb.web.mail.controller;

/**
 *
 * @author DUYNGUYEN
 */
import java.io.File;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Properties;
import javax.mail.Address;
import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import queenb.net.entitybean.N100;
import queenb.web.crm.store.CustomerStore;
import queenb.web.mail.store.M575Store;
import queenb.web.sms.store.M300Store;
import queenb.web.util.Util;

public class SchedulerJob implements Job {

    private CustomerStore store;
    private M575Store m575store;

    @Override
    public void execute(JobExecutionContext context)
            throws JobExecutionException {
        System.out.println("*******************************************************************");
        System.out.println("**************Schedu quart is excuted ! " + new Date() + "****************************");
        System.out.println("*******************************************************************");
        store = new CustomerStore();
        m575store = new M575Store();
        List<N100> listSendMail = store.getListCustomerBirthdaySendMail(7, 99, "admin@sgt.com");
        List<N100> listSendSMS = store.getListCustomerBirthdaySendSMS(7, 99, "admin@sgt.com");
        //send list mail
        if (listSendMail != null) {
            if (listSendMail.size() > 0) {
                for (N100 n100 : listSendMail) {
                    try {
                        int id = n100.getPn100();
                        String email = n100.getNv120();
                        String subject = "Thư chúc mừng sinh nhật - SAIGON TRAVEL !";
                        String pronous = "Quý khách";
                        if (n100.getNv107().equals("Nam")) {
                            pronous = "Ông";
                        } else if (n100.getNv107().equals("Nữ")) {
                            pronous = "Bà";
                        }
                        String content = "Kính gửi " + pronous + ": " + n100.getHoten() + " <br> ---- nội dung -----";
                        System.out.println(id + " : " + email + " : " + subject + " : " + content);

                        //sendmail
                        sendMailBcc(id, email, subject, content, 0);
                        //savemail
                        //int roweffect = m575store.updateM575By(7, 99, 0, 0, 1, subject, null, content, null, Util.toSQLDate(new Date()), 0, "E", "admin@sgt.com");
                    } catch (Exception e) {
                        System.out.println("Error from sendmail to cus auto: " + e.getMessage());
                        continue;
                    }
                }
            }
        }

        //send SMS
        if (listSendSMS != null) {
            if (listSendSMS.size() > 0) {
                for (N100 n100 : listSendSMS) {
                    try {
                        int id = n100.getPn100();
                        String phone = n100.getNv121();
                        String subject = "Thư chúc mừng sinh nhật - SAIGON TRAVEL !";
                        String pronous = "Quý khách";
                        if (n100.getNv107().equals("Nam")) {
                            pronous = "Ông";
                        } else if (n100.getNv107().equals("Nữ")) {
                            pronous = "Bà";
                        }
                        String content = "Kính gửi " + pronous + ": " + n100.getHoten() + " <br> ---- nội dung -----";
                        System.out.println(id + " : " + phone + " : " + subject + " : " + content);

                        //sendmail

                        //savemail
                    } catch (Exception e) {
                        System.out.println("Error from sendsms to cus auto: " + e.getMessage());
                        continue;
                    }
                }
            }
        }

        System.out.println("*******************************************************************");
        System.out.println("**************Schedu quart is excuted end! ************************");
        System.out.println("*******************************************************************");

    }

    public void sendMailBcc(int idcus, String email, String subject, String content, int roweffect) {
        try {
            String SMTP_AUTH_USER = "saotitan.sao@gmail.com";//your email address
            String SMTP_AUTH_PWD = "saotitan2011";//your password
            String SMTP_HOST_NAME = "smtp.gmail.com";
            int SMTP_HOST_PORT = 465;

            Properties props = new Properties();

            props.put("mail.transport.protocol", "smtps");
            props.put("mail.smtp.host", SMTP_HOST_NAME);
            props.put("mail.smtps.auth", "true");
            props.put("mail.smtp.starttls.enable", "true");
            props.put("mail.smtps.from", "kakaka@machine");

            Session mailSession = Session.getDefaultInstance(props);
            mailSession.setDebug(true);
            Transport transport = mailSession.getTransport();
            System.out.append("---------------1\n");

            MimeMessage message = new MimeMessage(mailSession);
            message.setSubject(subject, "utf-8");
            MimeMultipart multipart = new MimeMultipart("mixed");

//            for (int i = 0; i < files.size(); i++) {
//                MimeBodyPart messageBodyPart1 = new MimeBodyPart();
//                messageBodyPart1.attachFile(files.get(i).getPath());
//                multipart.addBodyPart(messageBodyPart1);
//            }
            message.setContent(multipart);
            message.addRecipient(Message.RecipientType.BCC, new InternetAddress(email));

            transport.connect(SMTP_HOST_NAME, SMTP_HOST_PORT, SMTP_AUTH_USER, SMTP_AUTH_PWD);
            System.out.append("---------------2\n");

            MimeBodyPart textMBCC = new MimeBodyPart();
            textMBCC.setContent(content, "text/html; charset=utf-8");
            Address[] st = new Address[1];
            st[0] = new InternetAddress(email);
            transport.sendMessage(message, st);

            transport.close();

//             save mail 
//            if (roweffect > 0) {
//                m575Inf.updateM275(roweffect, roweffect, idcus, "A", "TO", "E", "S", null, getLogin().getUserName());
//            }




        } catch (Exception e) {
            System.out.println("--D: Error from sendmailBCC auto: " + e.getMessage());
//            chkMess = true;
        }
    }
}
